package net.rock.eshop.service;

import net.rock.eshop.entity.Book;
import net.rock.eshop.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {
    @Autowired
    BookMapper bookMapper;

    static final int ITEMS_PER_PAGE = 100;

    // page 从0开始
    public List<Book> fetchBooks(Integer categoryId, Integer page) {
        Integer offset = null;
        if (page != null) {
            offset = page * ITEMS_PER_PAGE;
        }

        return bookMapper.fetchBooks(categoryId, offset);
    }

    public Book fetchBookById(int id) {
        return bookMapper.fetchBookById(id);
    }

    public void add(Book book) {
        bookMapper.add(book);
    }

    public void del(int id) {
        bookMapper.del(id);
    }

    public void edit(Book book) {
        bookMapper.edit(book);
    }

    public List<Book> searchBooks(String keyword) {
        return bookMapper.searchBooks(keyword);
    }

}
